<!doctype html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
	<meta charset="utf-8">
	<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
	<title>登录 - OAuth2认证服务</title>
	<link rel="stylesheet"
		href="../static/assets/bootstrap/css/bootstrap.min.css"
		th:href="@{/assets/bootstrap/css/bootstrap.min.css}" />
    <link rel="stylesheet"
          href="../static/assets/tac/css/tac.css"
          th:href="@{/assets/tac/css/tac.css}" />
    <link rel="stylesheet" 
          href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.15.4/css/all.min.css" 
          integrity="sha512-1ycn6IcaQQ40/MKBW2W4Rhis/DbILU74C1vSrLJxCq57o941Ym01SwNsOMqvEBFlcgUa6xLiPY/NS5R+E6ztJQ==" 
          crossorigin="anonymous" referrerpolicy="no-referrer" />
    <style>
        body {
            background-image: url('/bgimages/e7e619d5e087b8e1913c619881246800.jpeg');
            background-size: cover;
            background-position: center;
            background-attachment: fixed;
            height: 100vh;
            display: flex;
            align-items: center;
            justify-content: center;
            margin: 0;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }
        
        .login-container {
            background-color: rgba(255, 255, 255, 0.9);
            border-radius: 10px;
            box-shadow: 0 10px 25px rgba(0, 0, 0, 0.2);
            padding: 30px;
            max-width: 400px;
            width: 100%;
            animation: fadeIn 0.8s ease-in-out;
        }
        
        @keyframes fadeIn {
            from { opacity: 0; transform: translateY(-20px); }
            to { opacity: 1; transform: translateY(0); }
        }
        
        .login-header {
            text-align: center;
            margin-bottom: 25px;
        }
        
        .login-header h3 {
            color: #2c3e50;
            font-weight: 600;
            margin-bottom: 5px;
        }
        
        .login-header p {
            color: #7f8c8d;
            font-size: 14px;
        }
        
        .form-control {
            border-radius: 50px;
            padding: 12px 20px;
            height: auto;
            box-shadow: none;
            border: 1px solid #ddd;
            margin-bottom: 15px;
            transition: all 0.3s;
        }
        
        .form-control:focus {
            border-color: #4e73df;
            box-shadow: 0 0 0 0.2rem rgba(78, 115, 223, 0.25);
        }
        
        .input-group {
            position: relative;
            margin-bottom: 20px;
        }
        
        .input-icon {
            position: absolute;
            left: 15px;
            top: 14px;
            color: #aaa;
            z-index: 10;
        }
        
        .input-with-icon {
            padding-left: 45px;
        }
        
        .btn-login {
            border-radius: 50px;
            padding: 12px 20px;
            font-weight: 600;
            letter-spacing: 0.5px;
            background-color: #4e73df;
            border-color: #4e73df;
            width: 100%;
            margin-bottom: 15px;
            transition: all 0.3s;
        }
        
        .btn-login:hover {
            background-color: #3756a4;
            border-color: #3756a4;
            transform: translateY(-2px);
            box-shadow: 0 5px 15px rgba(78, 115, 223, 0.4);
        }
        
        .btn-signup {
            border-radius: 50px;
            padding: 10px 20px;
            font-weight: 500;
            background-color: transparent;
            border: 1px solid #4e73df;
            color: #4e73df;
            width: 100%;
            transition: all 0.3s;
        }
        
        .btn-signup:hover {
            background-color: #4e73df;
            color: white;
        }
        
        .alert-danger {
            border-radius: 5px;
            font-size: 14px;
            padding: 10px;
        }
        
        #captcha-box {
            margin: 15px 0;
        }
        
        .footer-text {
            text-align: center;
            font-size: 13px;
            color: #7f8c8d;
            margin-top: 15px;
        }
        
        .footer-text a {
            color: #4e73df;
            text-decoration: none;
        }
        
        .footer-text a:hover {
            text-decoration: underline;
        }
    </style>
</head>

<body>
    <div class="container">
        <div class="row justify-content-center">
            <div class="col-md-6 col-lg-5">
                <div class="login-container">
                    <div class="login-header">
                        <h3>OAuth2认证服务</h3>
                        <p>请登录您的账号</p>
                    </div>
                    
                    <form id="signInForm" action="/security_check" method="post">
                        <input type="hidden" name="graphId" id="graphId" />
                        
                        <div th:if="${error!=null && error!=''}" class="alert alert-danger" role="alert">
                            <i class="fas fa-exclamation-circle mr-2"></i>
                            <span th:text="${error}"></span>
                        </div>
                        
                        <div class="input-group">
                            <span class="input-icon">
                                <i class="fas fa-user"></i>
                            </span>
                            <input type="text" name="username" class="form-control input-with-icon"
                                id="username" required="required" placeholder="用户名" autocomplete="username" />
                        </div>
                        
                        <div class="input-group">
                            <span class="input-icon">
                                <i class="fas fa-lock"></i>
                            </span>
                            <input type="password" name="password" class="form-control input-with-icon"
                                id="password" required="required" placeholder="密码" autocomplete="current-password" />
                        </div>
                        
                        <div id="captcha-box"></div>
                        
                        <div class="form-group mt-4">
                            <button type="submit" class="btn btn-primary btn-login">
                                <i class="fas fa-sign-in-alt mr-2"></i> 登录
                            </button>
                            <a href="/signUp" class="btn btn-signup">
                                <i class="fas fa-user-plus mr-2"></i> 注册账号
                            </a>
                        </div>
                        
                        <div class="footer-text">
                            <p>安全认证服务 &copy; 2025</p>
                        </div>
                    </form>
                </div>
            </div>
        </div>
    </div>

	<script src="../static/assets/jquery.min.js"
		th:src="@{/assets/jquery.min.js}"></script>
	<script src="../static/assets/bootstrap/js/bootstrap.min.js"
		th:src="@{/assets/bootstrap/js/bootstrap.min.js}"></script>
	<script th:src="@{/assets/localforage.min.js}"
		src="../static/assets/localforage.min.js"></script>
    <script src="../static//assets/tac/js/tac.min.js"
            th:src="@{/assets/tac/js/tac.min.js}"></script>
	<script th:inline="javascript">
    /*<![CDATA[*/
    	var captchaPassed = false;
        $(document).ready(function () {

            const config = {
               // 生成接口 (必选项,必须配置, 要符合tianai-captcha默认验证码生成接口规范) 参考 https://gitee.com/tianai/tianai-captcha-demo
               requestCaptchaDataUrl: "/captcha/gen",
               // 验证接口 (必选项,必须配置, 要符合tianai-captcha默认验证码校验接口规范) 参考 https://gitee.com/tianai/tianai-captcha-demo
               validCaptchaUrl: "/captcha/check",
               // 验证码绑定的div块 (必选项,必须配置)
               bindEl: "#captcha-box",
               // 验证成功回调函数(必选项,必须配置)
               validSuccess: (res, c, tac) => {
                   // todo 这里res为后端返回的数据，一般返回验证成功的token， 进行登录或其它操作时，要把返回的token传给后端进行二次验证
                   console.log("验证成功，后端返回的数据为", res);
                  // 调用登录方法进行登录 这里login登录方法为自己的登录函数
                  //   this.login(res.captchaToken)
                 captchaPassed = true;
                 $("#signInForm").submit();
                   // 销毁验证码服务
                 tac.destroyWindow();
               },
               // 验证失败的回调函数(可忽略，如果不自定义 validFail 方法时，会使用默认的)
               validFail: (res, c, tac) => {
                   // 验证失败后重新拉取验证码
                   tac.reloadCaptcha();
               }
            }

            localforage.getItem('username').then(function (value) {
                // This code runs once the value has been loaded
                // from the offline store.
                if (value != null && value != "") {
                    $("#username").val(value);
                    $("#rememberUsername").prop("checked", "checked");
                    $("#password").focus();
                }

            }).catch(function (err) {
                // This code runs if there were any errors
                console.log(err);
            });

            $("#signInForm").on("submit", function (event) {
                if(captchaPassed == false){
                    event.preventDefault();
                    // 创建 TAC 启动验证码服务,调用该方法后会在指定的div块中渲染出验证码
                    new TAC(config).init();
                    return false;
                }

                if ($("#rememberUsername").prop("checked")) {
                    localforage.setItem('username', $("#username").val()).then(function (value) {
                        // Do other things once the value has been saved.
                    }).catch(function (err) {
                        // This code runs if there were any errors
                        console.log(err);
                    });
                } else {
                    localforage.removeItem('username').then(function (value) {
                        // Do other things once the value has been saved.
                    }).catch(function (err) {
                        // This code runs if there were any errors
                        console.log(err);
                    });
                }
            });
            
            // 添加输入框焦点效果
            $(".form-control").focus(function() {
                $(this).parent().find(".input-icon i").css("color", "#4e73df");
            }).blur(function() {
                $(this).parent().find(".input-icon i").css("color", "#aaa");
            });
        });
    /*]]>*/
</script>

</body>
</html>
